package lianxi;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class demo7 {
    public int findKth(int[] a, int n, int K) {
        // write code here

        PriorityQueue<Integer> queue=new java.util.PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        for (int i = 0; i < a.length; i++) {
            if(queue.size()<K){
                queue.add(a[i]);
            }
            else if(queue.peek()<a[i]){
                queue.poll();
                queue.add(a[i]);
            }
        }

        return queue.peek();
    }

}
